import api from '../../utils/api';
import { myRequest } from '../../utils/request';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        content: "",
        left: 8,  // 搜索类型向左偏移
        searchType: 1,  // 1：音乐  2：专辑  3：歌单
        searchList: []  // 搜索结果 
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.searchMusicList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    // 清除搜索框输入
    cleanInput() {
        this.setData({
          content : ''
        });
    },

    // 搜素
    search() {
      const { searchType } = this.data;
      if(searchType === 1) {
        // 搜索音乐
        this.searchMusicList();
      } else if (searchType === 2) {
        this.searchAlbumList();
      } else if (searchType === 3) {
        this.searchMusicMenuList();
      } else if (searchType === 4) {
        this.searchSingerList();
      }
    },

    // 跳转音乐播放页面
    goToMusicPlayer(event) {
      const data = event.currentTarget.dataset; 
      let music = data.music;
      delete music['viewPhoto'];
      wx.navigateTo({
        url: '/pages/player/player?music=' + JSON.stringify(music),
      })
    },

    // 切换搜索类型
    changeTypeByTap(event) {
        let id = event.currentTarget.id;
        id = Number(id);
        if (id === 1) {
          this.setData({
            left: 8,
            searchType: id
          })
          this.searchMusicList();
        } else if (id === 2) {
          this.setData({
            left: 33,
            searchType: id
          })
          this.searchAlbumList();
        } else if(id === 3) {
          this.setData({
            left: 58,
            searchType: id
          })
          this.searchMusicMenuList();
        } else if(id === 4) {
          this.setData({
            left: 83,
            searchType: id
          })
          this.searchSingerList();
        }
    },

    // 跳转到歌手列表
    goToSingerList(event) {
      const data = event.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/musicList/musicList?type=3&searchId=${data.id}`
      })
    },

    // 跳转到专辑列表
    goToAlbumMusicList(event) {
      const data = event.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/musicList/musicList?type=1&searchId=${data.id}`
      })
    },

    // 跳转到歌单列表
    goToMusicMenuList(event) {
      const data = event.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/musicList/musicList?type=2&searchId=${data.id}`
      })
    },

    // 搜索音乐
    async searchMusicList() {
      const res = await myRequest({
          url: '/app/music/search',
          method: 'POST',
          data: {name: this.data.content}
      });
      if(res.data.code === 0) {
          let result = res.data.data;
          result.forEach(e => {
              e.viewPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
          });
          this.setData({searchList: res.data.data});
      }
    },

     // 搜索专辑
     async searchAlbumList() {
      const res = await myRequest({
          url: '/app/album/search',
          method: 'POST',
          data: {name: this.data.content}
      });
      if(res.data.code === 0) {
          let result = res.data.data;
          result.forEach(e => {
              e.viewPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
          });
          this.setData({searchList: res.data.data});
      }
    },


    // 搜索歌手
    async searchSingerList() {
      const res = await myRequest({
          url: '/app/singer/search',
          method: 'POST',
          data: {name: this.data.content}
      });
      if(res.data.code === 0) {
          let result = res.data.data;
          result.forEach(e => {
              e.viewPhoto = api.BASE_URL + '/photo/view?filename=' + e.headPic;
          });
          this.setData({searchList: res.data.data});
      }
    },

    // 搜索歌单
    async searchMusicMenuList() {
      const res = await myRequest({
          url: '/app/music/menu/search',
          method: 'POST',
          data: {name: this.data.content}
      });
      if(res.data.code === 0) {
          let result = res.data.data;
          result.forEach(e => {
              e.viewPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
          });
          this.setData({searchList: res.data.data});
      }
    },

    // 表单项内容发生改变的回调
    handleInput(event) {
        let type = event.currentTarget.dataset.type;  
        this.setData({
            // type是变量，对象里面操作属性用中括号
            [type]: event.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})